package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Advice;
import com.itheima.pojo.Exercise;
import com.itheima.pojo.Food;
import com.itheima.service.AdviceService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/advice")
public class AdviceController {
    @Reference
    private AdviceService adviceService;

    @RequestMapping("/add")
    public Result add(@RequestParam("foodIds") Integer[] foodIds, @RequestParam("exerciseIds") Integer[] exerciseIds,@RequestBody Advice advice) {
        try {
            adviceService.add(foodIds,exerciseIds,advice);
            return new Result(true, MessageConstant.ADD_ADVICE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ADVICE_FAIL);
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestParam("foodIds") Integer[] foodIds, @RequestParam("exerciseIds") Integer[] exerciseIds,@RequestBody Advice advice) {
        try {
            adviceService.edit(foodIds,exerciseIds,advice);
            return new Result(true, MessageConstant.ADD_ADVICE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ADVICE_FAIL);
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        Advice advice = adviceService.findById(id);
        return new Result(true,MessageConstant.GET_exercise_SUCCESS,advice);
    }

    @RequestMapping("/findExerciseById")
    public Result findExerciseById(Integer id){
        try {
            List<Integer> exerciseList = adviceService.findExerciseById(id);
            return new Result(true,MessageConstant.GET_exercise_LIST_SUCCESS,exerciseList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_exercise_FAIL);
        }
    }

    @RequestMapping("/findFoodById")
    public Result findFoodById(Integer id){
        try {
            List<Integer> exerciseList = adviceService.findFoodById(id);
            return new Result(true,MessageConstant.GET_FOOD_LIST_SUCCESS,exerciseList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_exercise_LIST_FAIL);
        }
    }
}
